package com.lpwa.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.lpwa.common.annotation.DataScope;
import com.lpwa.common.config.Global;
import com.lpwa.common.utils.StringUtils;
import com.lpwa.common.utils.http.HttpUtils;
import com.lpwa.system.domain.locatorInfo.TdgsLocationInfo;
import com.lpwa.system.domain.locatorInfo.TdgsLocators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lpwa.system.mapper.LocatorMapper;
import com.lpwa.system.domain.Locator;
import com.lpwa.system.service.ILocatorService;
import com.lpwa.common.core.text.Convert;

/**
 * 基站 服务层实现
 * 
 * @author tanmu
 * @date 2019-06-10
 */
@Service
public class LocatorServiceImpl implements ILocatorService
{
	@Autowired
	private LocatorMapper locatorMapper;

	/**
     * 查询基站信息
     * 
     * @param locatorId 基站ID
     * @return 基站信息
     */
    @Override
	public Locator selectLocatorById(String locatorId)
	{
	    return locatorMapper.selectLocatorById(locatorId);
	}
	
	/**
     * 查询基站列表
     * 
     * @param locator 基站信息
     * @return 基站集合
     */
	@Override
	@DataScope(tableAlias = "a")
	public List<Locator> selectLocatorList(Locator locator)
	{
		//把基站类型和型号放进去
	    return locatorMapper.selectLocatorList(locator);
	}
	
    /**
     * 新增基站
     * 
     * @param locator 基站信息
     * @return 结果
     */
	@Override
	public int insertLocator(Locator locator)
	{
	    return locatorMapper.insertLocator(locator);
	}
	
	/**
     * 修改基站
     * 
     * @param locator 基站信息
     * @return 结果
     */
	@Override
	public int updateLocator(Locator locator)
	{
	    return locatorMapper.updateLocator(locator);
	}

	/**
     * 删除基站对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteLocatorByIds(String ids)
	{
		return locatorMapper.deleteLocatorByIds(Convert.toStrArray(ids));
	}

	@Override
	public String checkLocatorIdUnique(String locatorId) {
		int count = locatorMapper.checkLocatorIdUnique(locatorId);
		if (count > 0)
		{
			return "1";
		}
		return "0";
	}

//	@Override
//	public void exportLocatorIdByApi() {
//		String Info = HttpUtils.sendPost(" http://10.35.76.224:8080/qpe/getLocatorInfo","humanReadable=true");
//		LocatorInfo locatorInfo = JSON.parseObject(Info, LocatorInfo.class);
//		List<Locators> locatorsList = locatorInfo.getLocators();
//		if ((!StringUtils.isEmpty(locatorsList))&&locatorsList.size()>0){
//		    for (Locators locators :locatorsList){
//                String locatorId = locators.getId();//基站Id
//                String connection = locators.getConnection();//链接状态
//                //校验locatorId是否存在
//				String strtus = checkLocatorIdUnique(locatorId);
//				if (strtus.equals("0")){//不存在则新增
//					Locator locator = new Locator();
//					locator.setLocatorId(locatorId);
//					if (connection.equals("ok")){//基站状态为在线
//						locator.setIsOnline(1);
//					}
//					locatorMapper.insertLocator(locator);
//				}
//            }
//        }
//	}
	@Override
	public void exportLocatorIdByApi() {
		String httpAddress = Global.getTdgsIpAndPort();
		String Info = HttpUtils.sendPost("http://"+httpAddress+"/tdgs/getLocatorInfo","1=1");
		TdgsLocationInfo locatorInfo = JSON.parseObject(Info, TdgsLocationInfo.class);
		List<TdgsLocators> locatorsList = locatorInfo.getLocators();
		if ((!StringUtils.isEmpty(locatorsList))&&locatorsList.size()>0){
			for (TdgsLocators locators :locatorsList){
				String locatorId = locators.getID();//基站Id
				String connection = locators.getConnection();//链接状态
				//校验locatorId是否存在
				String strtus = checkLocatorIdUnique(locatorId);
				if (strtus.equals("0")){//不存在则新增
					Locator locator = new Locator();
					locator.setLocatorId(locatorId);
					if (connection.equals("ok")){//基站状态为在线
						locator.setIsOnline(1);
					}
					locatorMapper.insertLocator(locator);
				}
			}
		}
	}
//	@Override
//	public void updateLocatorIsOnline() {
//		String Info = HttpUtils.sendPost(" http://10.35.76.224:8080/qpe/getLocatorInfo","humanReadable=true");
//		LocatorInfo locatorInfo = JSON.parseObject(Info, LocatorInfo.class);
//		List<Locators> locatorsList = locatorInfo.getLocators();
//		if ((!StringUtils.isEmpty(locatorsList))&&locatorsList.size()>0){
//			for (Locators locators :locatorsList){
//				Locator locator = new Locator();
//				String locatorId = locators.getId();//基站Id
//				locator.setLocatorId(locatorId);
//				String connection = locators.getConnection();//链接状态
//				if (connection.equals("ok")){//基站状态为在线
//						locator.setIsOnline(1);
//				}else {
//					locator.setIsOnline(0);
//				}
//				//校验locatorId是否存在
//				String strtus = checkLocatorIdUnique(locatorId);
//				if (strtus.equals("1")) {//存在则更新
//					locatorMapper.updateLocator(locator);
//				}
//			}
//		}
//	}

	@Override
	public void updateLocatorIsOnline() {
		String httpAddress = Global.getTdgsIpAndPort();
		String Info = HttpUtils.sendPost("http://"+httpAddress+"/tdgs/getLocatorInfo","1=1");
		TdgsLocationInfo locatorInfo = JSON.parseObject(Info, TdgsLocationInfo.class);
		List<TdgsLocators> locatorsList = locatorInfo.getLocators();

		if ((!StringUtils.isEmpty(locatorsList))&&locatorsList.size()>0){
			for (TdgsLocators locators :locatorsList){
				Locator locator = new Locator();
				String locatorId = locators.getID();//基站Id
				locator.setLocatorId(locatorId);
				String connection = locators.getConnection();//链接状态
				if (connection.equals("ok")){//基站状态为在线
					locator.setIsOnline(1);
				}else {
					locator.setIsOnline(0);
				}
				//校验locatorId是否存在
				String strtus = checkLocatorIdUnique(locatorId);
				if (strtus.equals("1")) {//存在则更新
					locatorMapper.updateLocator(locator);
				}
			}
		}
	}

}
